﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;


namespace MODULE_NHAPXUAT
{
    public partial class frm_nhaphang : Form
    {
        int currenrowindex;
        double trongluong;
        bool isloaded = false;
        String chedocan = constant.CANBANGTAY;//defaut
        nhapxuatDataContext context = cls_nhapxuat.getcontext();
        bool XOI = false;
        bool BI = false;
        bool OK = false;
        string strongluong="S??GS?-0000000???";
         //strongluong = "S??GS?-0000050???";
                //strongluong = "S??GS?+00050?0???";
        public frm_nhaphang()
        {
            InitializeComponent();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {


        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {


        }

       

        private void txtmachuhang_TextChanged(object sender, EventArgs e)
        {


        }

        private void txtmachuhang_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
               
                if (e.KeyCode == Keys.Enter)
                {
                    string machuhang = this.txtmachuhang.Text;
                    if (machuhang != "")
                    {
                        IEnumerable<DM_chuhang> chuhangs = from c in context.DM_chuhangs where c.machuhang.Equals(machuhang) select c;
                        if (chuhangs.Count() > 0)
                        {
                            DM_chuhang chuhang = chuhangs.ElementAt(0);
                            this.txttenchuhang.Text = chuhang.tenchuhang;
                            this.txtdiachi.Text = chuhang.diachi;

                        }
                        else
                        {
                            MODULE_TIENICH.cls_tienich cls = new MODULE_TIENICH.cls_tienich();
                            cls.ShowForm("frm_timkiemchuhang");

                            this.txtmachuhang.Text = cls._makhachhang;
                            chuhangs = from c in context.DM_chuhangs where c.machuhang.Equals(this.txtmachuhang.Text) select c;
                            if (chuhangs.Count() > 0)
                            {
                                DM_chuhang chuhang = chuhangs.ElementAt(0);
                                this.txttenchuhang.Text = chuhang.tenchuhang;
                                this.txtdiachi.Text = chuhang.diachi;
                            }
                        }
                    }
                    else
                    {

                        MODULE_TIENICH.cls_tienich cls = new MODULE_TIENICH.cls_tienich();
                        cls.ShowForm("frm_timkiemchuhang");

                        this.txtmachuhang.Text = cls._makhachhang;
                        IEnumerable<DM_chuhang> chuhangs = from c in context.DM_chuhangs where c.machuhang.Equals(this.txtmachuhang.Text) select c;
                        if (chuhangs.Count() > 0)
                        {
                            DM_chuhang chuhang = chuhangs.ElementAt(0);
                            this.txttenchuhang.Text = chuhang.tenchuhang;
                            this.txtdiachi.Text = chuhang.diachi;
                        }
                    }
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private bool validate()
        {
            
            bool flag = true;
            if (this.txtsophieu.Text == "")
            {
                flag = false;

            }

            if (this.txtmachuhang.Text == "")
            {
                flag = false;
                this.lbmachuhang.Text = "(*)";
            }
            else
            {
                this.lbmachuhang.Text = "";
            }
            if (this.txttenchuhang.Text == "")
            {
                flag = false;
                this.lbtenchuhang.Text = "(*)";
            }
            else
            {
                this.lbtenchuhang.Text = "";
            }
            if (this.txtsoxe.Text == "")
            {
                flag = false;
                this.lbsoxe.Text = "(*)";
            }
            else
            {
                this.lbsoxe.Text = "";
            }
            if (this.cbloaimi.Text == "")
            {
                flag = false;
                this.lbloaimi.Text = "(*)";
            }
            else
            {
                this.lbloaimi.Text = "";
            }
            return flag;
            
        }
        private void btSave_Click(object sender, EventArgs e)
        {
            try
            {

               
                NX_phieunhaphang_h nhaph = new NX_phieunhaphang_h();
                if (validate() == true)
                {
                    nhaph.sophieu = this.txtsophieu.Text;
                    nhaph.machuhang = this.txtmachuhang.Text;

                    string d, m, y;
                    string giovao, giora;
                    if (this.dtpgiovao.Value.Day >= 10)
                    {
                        d = this.dtpgiovao.Value.Day.ToString();
                    }
                    else
                    {
                        d = "0" + this.dtpgiovao.Value.Day.ToString();
                    }

                    if (this.dtpgiovao.Value.Month >= 10)
                    {
                        m = this.dtpgiovao.Value.Month.ToString();
                    }
                    else
                    {
                        m = "0" + this.dtpgiovao.Value.Month.ToString();
                    }
                    y = this.dtpgiovao.Value.Year.ToString();


                    giovao = d + "/" + m + "/" + y;
                    nhaph.giovao = giovao;

                    ////
                    if (this.dtpgiora.Value.Day >= 10)
                    {
                        d = this.dtpgiora.Value.Day.ToString();
                    }
                    else
                    {
                        d = "0" + this.dtpgiora.Value.Day.ToString();
                    }

                    if (this.dtpgiora.Value.Month >= 10)
                    {
                        m = this.dtpgiora.Value.Month.ToString();
                    }
                    else
                    {
                        m = "0" + this.dtpgiora.Value.Month.ToString();
                    }
                    y = this.dtpgiora.Value.Year.ToString();

                    giora = d + "/" + m + "/" + y;
                    nhaph.giora = giora;
                    nhaph.laixe = this.txtlaixe.Text;
                    nhaph.soxe = this.txtsoxe.Text;
                    nhaph.loaimi = cbloaimi.SelectedValue.ToString();
                    nhaph.nguoikiemhang = "";
                    nhaph.nguoinhaphang = "";
                    nhaph.nguoinhaplieu = "";
                    nhaph.tongsotienbangso = 0;
                    nhaph.tongsotienbangchu = "";

                    try
                    {
                        nhaph.tongtrongluong = double.Parse(txttongtrongluong.Text);
                    }
                    catch (Exception ex)
                    {
                        nhaph.tongtrongluong = 0;
                    }
                    try
                    {
                        nhaph.trongluongxe = double.Parse(this.txttrongluongxe.Text);
                    }
                    catch (Exception ex)
                    {
                        nhaph.trongluongxe = 0;
                    }
                    nhaph.trongluongbitru = 0;
                    nhaph.trongluongthanhtoan = 0;
                    IEnumerable<NX_phieunhaphang_h> nhaphs = from c in context.NX_phieunhaphang_hs where c.sophieu.Equals(nhaph.sophieu) select c;
                    bool isexists = false;
                    if (nhaphs.Count() > 0)
                    {
                        isexists = true;
                    }
                    try
                    {
                        if (isexists == true)
                        {
                            foreach (NX_phieunhaphang_h h in nhaphs)
                            {
                                h.machuhang = nhaph.machuhang;
                                h.soxe = nhaph.soxe;
                                h.laixe = nhaph.laixe;
                                h.giovao = nhaph.giovao;
                                h.giora = nhaph.giora;
                                h.loaimi = nhaph.loaimi;
                                h.tongtrongluong = nhaph.tongtrongluong;
                                h.trongluongxe = nhaph.trongluongxe;
                                context.SubmitChanges();
                            }
                        }
                        else
                        {
                            context.NX_phieunhaphang_hs.InsertOnSubmit(nhaph);
                            context.SubmitChanges();
                        }
                        MessageBox.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Không lưu được.Lỗi: " + ex.Message, "Thông báo", MessageBoxButtons.OK);
                    }

                }
                //getdata();
                loaddanhsachphieunhap();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

       

        private void rdmituoi_Click(object sender, EventArgs e)
        {
           

        }

        private void rdmikho_Click(object sender, EventArgs e)
        {
           
            
        }
        private void xoathongbao()
        {
            this.lbdiachi.Text = "";
            this.lbloaimi.Text = "";
            this.lbmachuhang.Text = "";
            this.lbsoxe.Text = "";
            this.lbtenchuhang.Text = "";
        }

        private void add()
        {
            IEnumerable<P_getsophieunhapResult> phieunhaps = context.P_getsophieunhap().AsEnumerable();
            string sophieu = "";
            if (phieunhaps.Count() > 0)
            {
                sophieu = phieunhaps.ElementAt(0).sophieu;
            }
            this.txtsophieu.Text = sophieu;
            this.btSave.Enabled = true;

        }
        private void phatsinhsophieu()
        {

            IEnumerable<P_getsophieunhapResult> phieunhaps = context.P_getsophieunhap().AsEnumerable();
            string sophieu = "";
            sophieu = phieunhaps.ElementAt(0).sophieu;
            this.txtsophieu.Text = sophieu;
            
        }
        private void loaddanhsachphieunhap()
        {
            string tungay, denngay;
            string d, m, y;

            if (this.dtptungay.Value.Day >= 10)
            {
                d = this.dtptungay.Value.Day.ToString();
            }
            else
            {
                d = "0" + this.dtptungay.Value.Day.ToString();
            }

            if (this.dtptungay.Value.Month >= 10)
            {
                m = this.dtptungay.Value.Month.ToString();
            }
            else
            {
                m = "0" + this.dtptungay.Value.Month.ToString();
            }
            y = this.dtptungay.Value.Year.ToString();
            tungay = d + "/" + m + "/" + y;
            if (this.dtpdenngay.Value.Day >= 10)
            {
                d = this.dtpdenngay.Value.Day.ToString();
            }
            else
            {
                d = "0" + this.dtpdenngay.Value.Day.ToString();
            }

            if (this.dtpdenngay.Value.Month >= 10)
            {
                m = this.dtpdenngay.Value.Month.ToString();
            }
            else
            {
                m = "0" + this.dtpdenngay.Value.Month.ToString();
            }
            y = this.dtpdenngay.Value.Year.ToString();
            denngay = d + "/" + m + "/" + y;



            List<NX_danhsachphieunhapResult> list = context.NX_danhsachphieunhap(tungay, denngay).ToList();
            this.grdata.Rows.Clear();
            for (int i = 0; i < list.Count(); i++)
            {
                NX_danhsachphieunhapResult item = list.ElementAt(i);

                if (item.sophieu != null)
                {

                    this.grdata.Rows.Add();

                    this.grdata.Rows[i].Cells["sophieu"].Value = item.sophieu;
                    this.grdata.Rows[i].Cells["soxe"].Value = item.soxe;
                    IEnumerable<DM_chuhang> chuhangs = from c in context.DM_chuhangs where c.machuhang.Equals(item.machuhang) select c;
                    if (chuhangs.Count() > 0)
                    {
                        this.grdata.Rows[i].Cells["tenchuhang"].Value = chuhangs.ElementAt(0).tenchuhang;//ch.tenchuhang;
                    }
                    this.grdata.Rows[i].Cells["tongtrongluong"].Value = item.tongtrongluong;
                    this.grdata.Rows[i].Cells["trongluongxe"].Value = item.trongluongxe;
                    //Kiem tra tinh trang phieu

                    grdata.Rows[i].DefaultCellStyle.ForeColor = Color.Red;//Mau phieu moi nhap
                    IEnumerable<NX_phieunhaphang_l> kiemdinhs = from c in context.NX_phieunhaphang_ls where c.sophieu.Equals(item.sophieu) select c;
                    if (kiemdinhs.Count() > 0)
                    {
                        grdata.Rows[i].Cells["chk"].ReadOnly = true;

                        grdata.Rows[i].DefaultCellStyle.ForeColor = Color.MediumBlue;//System.Drawing.Color.Red;
                    }
                    IEnumerable<KT_hoadon_l> hoadons = from c in context.KT_hoadon_ls where c.sophieunhap.Equals(item.sophieu) select c;
                    if (hoadons.Count() > 0)
                    {
                        grdata.Rows[i].Cells["chk"].ReadOnly = true;
                        grdata.Rows[i].DefaultCellStyle.ForeColor = Color.Black;//System.Drawing.Color.Red;

                        //flag = false;
                    }


                }


            }


        }
        //private void getdata()
        //{
        //    try
        //    {

        //        this.cbloaimi.DataSource = context.DM_loaimis;
        //        this.cbloaimi.DisplayMember = "tenloai";
        //        this.cbloaimi.ValueMember = "maloai";
        //        //IEnumerable<p_getdateResult> serverdates = context.p_getdate();
        //        DateTime serverdate = context.p_getdate().ElementAt(0).serverdatetime;
        //        this.dtpdenngay.Value = serverdate;
        //        this.dtptungay.Value = serverdate;
        //        //IEnumerable<NX_danhsachphieunhapResult> t = from c in context.NX_danhsachphieunhap("", "") select c;
        //        IEnumerable<NX_phieunhaphang_h> list = from c in context.NX_phieunhaphang_hs
                                                       
        //                                               where DateTime.Parse(c.giovao) >= this.dtptungay.Value
        //                                               where DateTime.Parse(c.giovao) <= this.dtpdenngay.Value
        //                                               select c;
                
        //        this.grdata.Rows.Clear();
        //        if (list != null)
        //        {
        //            for (int i = 0; i < list.Count(); i++)
        //            {
        //                NX_phieunhaphang_h item = list.ElementAt(i);

        //                if (item.sophieu != null)
        //                {
        //                    try
        //                    {

        //                        this.grdata.Rows.Add();
        //                        if (item.sophieu != null)
        //                        {
        //                            this.grdata.Rows[i].Cells["sophieu"].Value = item.sophieu;
        //                        }
        //                        else
        //                        {
        //                            this.grdata.Rows[i].Cells["sophieu"].Value = "";
        //                        }
        //                        if (item.soxe != null)
        //                        {
        //                            this.grdata.Rows[i].Cells["soxe"].Value = item.soxe;
        //                        }

        //                        else
        //                        {
        //                            this.grdata.Rows[i].Cells["soxe"].Value = "";
        //                        }
        //                        if (item.DM_chuhang.tenchuhang != null)
        //                        {
        //                            this.grdata.Rows[i].Cells["tenchuhang"].Value = item.DM_chuhang.tenchuhang;//ch.tenchuhang;
        //                        }
        //                        else
        //                        {
        //                            this.grdata.Rows[i].Cells["tenchuhang"].Value = "";
        //                        }
        //                        if (item.tongtrongluong != null)
        //                        {
        //                            this.grdata.Rows[i].Cells["tongtrongluong"].Value = item.tongtrongluong.ToString();
        //                        }
        //                        else
        //                        {
        //                            this.grdata.Rows[i].Cells["tongtrongluong"].Value = "0";
        //                        }
        //                        if (item.trongluongxe.ToString() != null)
        //                        {
        //                            this.grdata.Rows[i].Cells["trongluongxe"].Value = item.trongluongxe.ToString();
        //                        }
        //                        else
        //                        {
        //                            this.grdata.Rows[i].Cells["trongluongxe"].Value = "";
        //                        }
        //                        //Kiem tra tinh trang phieu
        //                    }
        //                    catch (Exception ex)
        //                    {
        //                        MessageBox.Show("Loi gan du lieu cho grid");
        //                    }
        //                }

        //                    grdata.Rows[i].DefaultCellStyle.ForeColor = Color.Red;//Mau phieu moi nhap
        //                    IEnumerable<NX_phieunhaphang_l> kiemdinhs = from c in context.NX_phieunhaphang_ls where c.sophieu.Equals(item.sophieu) select c;
        //                    if (kiemdinhs != null)
        //                    {
        //                        if (kiemdinhs.Count() > 0)
        //                        {
        //                            grdata.Rows[i].Cells["chk"].ReadOnly = true;

        //                            grdata.Rows[i].DefaultCellStyle.ForeColor = Color.MediumBlue;//System.Drawing.Color.Red;
        //                        }
        //                    }
        //                    IEnumerable<KT_hoadon_l> hoadons = from c in context.KT_hoadon_ls where c.sophieunhap.Equals(item.sophieu) select c;
        //                    if (hoadons != null)
        //                    {
        //                        if (hoadons.Count() > 0)
        //                        {
        //                            grdata.Rows[i].Cells["chk"].ReadOnly = true;
        //                            grdata.Rows[i].DefaultCellStyle.ForeColor = Color.Black;//System.Drawing.Color.Red;

        //                            //flag = false;
        //                        }
        //                    }


        //                }

        //            }
        //        }
            
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show("Loi trong qua trinh lieu du lieu cho luoi" + ex.Message);
        //    }

    
        //}
        public Boolean iscantudong()
        {
            
            IEnumerable<thamso> thamsos = from c in context.thamsos select c;
            string loaihinhcan="";
            MODULE_QUANTRIHETHONG.constant constant = new MODULE_QUANTRIHETHONG.constant();


            for (int i = 0; i < thamsos.Count(); i++)
            {
                thamso thamso = thamsos.ElementAt(i);
                if (thamso.tenthamso.ToUpper() == MODULE_QUANTRIHETHONG.constant.LOAIHINHCAN)
                {
                    loaihinhcan = thamso.giatri;
                    
                }

            }
            if (loaihinhcan == MODULE_QUANTRIHETHONG.constant.CANTUDONG)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void frm_nhaphang_Load(object sender, EventArgs e)
        {

            try
            {
                currenrowindex = 0;
                phatsinhsophieu();
                this.cbloaimi.DataSource = context.DM_loaimis;
                this.cbloaimi.DisplayMember = "tenloai";
                this.cbloaimi.ValueMember = "maloai";
                //IEnumerable<p_getdateResult> serverdates = context.p_getdate();
                DateTime serverdate = context.p_getdate().ElementAt(0).serverdatetime;
                this.dtpdenngay.Value = serverdate;
                this.dtptungay.Value = serverdate;
                //getdata();
                loaddanhsachphieunhap();
                //xoathongbao();
                
                //Mo cong com
                IEnumerable<thamso> thamsos = from c in context.thamsos where c.tenthamso.Equals(constant.LOAIHINHCAN) select c;
                thamso thamso = thamsos.ElementAt(0);
                chedocan = constant.CANBANGTAY;//defaut
                try
                {
                    chedocan = thamso.giatri;
                }
                catch
                {
                }

                if (chedocan == constant.CANTUDONG)
                {
                    this.bttrongluongxoi.Enabled = true;
                    this.bttrongluongbi.Enabled = true;
                    this.txttongtrongluong.ReadOnly = true;
                    this.txttrongluongxe.ReadOnly = true;
                    try
                    {
                        if (!this.serialPort1.IsOpen)
                        {
                            this.serialPort1.Open();
                        }
                        lbchedocan.Text = "Chế độ cân:tự động";
                        this.lbstatuscom.Text = "Cổng com đã sẵn sàng.";
                    }
                    catch (Exception ex)
                    {
                        this.lbstatuscom.Text = "Không mở được cổng com.";
                    }
                }
                else
                {
                    lbchedocan.Text = "Chế độ cân:bằng tay";
                    this.lbstatuscom.Text = "";
                    this.bttrongluongxoi.Enabled = false;
                    this.bttrongluongbi.Enabled = false;
                    this.txttongtrongluong.ReadOnly = false;
                    this.txttrongluongxe.ReadOnly = false;

                }
                isloaded = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void label36_Click(object sender, EventArgs e)
        {

        }

        private void label37_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.grdata.Rows[currenrowindex].Selected = false;

                currenrowindex = 0;


                this.grdata.Rows[currenrowindex].Selected = true;

                getrowdata();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            try
            {

                Delete();
                //getdata();
                loaddanhsachphieunhap();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void Delete()
        {
            try
            {
            ArrayList arrphieu = new ArrayList();
            for (int i = 0; i < grdata.RowCount; i++)
            {
                // DataGridViewCheckBoxCell c = (DataGridViewCheckBoxCell)this.grdata.Rows[i].Cells["sophieu"];
                //int v;



                if (this.grdata.Rows[i].Cells["chk"].Value != null && this.grdata.Rows[i].Cells["chk"].Value != "")
                {
                    bool v = (bool)this.grdata.Rows[i].Cells["chk"].Value;
                    if (v == true)
                    {
                        if (this.grdata.Rows[i].Cells["sophieu"].Value != null)
                        {
                            string sophieu = this.grdata.Rows[i].Cells["sophieu"].Value.ToString();
                            //Deleted(sophieu);
                            arrphieu.Add(sophieu);
                        }
                    }



                }


            }
            ArrayList arrdel = new ArrayList();
            if (arrphieu.Count > 0)
            {
                for (int j = 0; j < arrphieu.Count; j++)
                {
                    //Kiem tra phieu da kiem dinh và da thanh toan chua.
                    bool flag = true;
                    IEnumerable<NX_phieunhaphang_l> kiemdinhs=from c in context.NX_phieunhaphang_ls where c.sophieu.Equals( arrphieu[j].ToString())select c;
                    


                    DeletedRow(arrphieu[j].ToString());
                }

                MessageBox.Show("Xóa thành công", "Thông báo",MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show("Chọn và đánh dấu ít nhất 1 phiếu cần xóa.");
            }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Không xóa được.Lỗi: " + ex.Message, "Thông báo", MessageBoxButtons.OK);
            }
        }
        private void DeletedRow(string sophieu)
        {
            
            
               
                NX_phieunhaphang_h h = new NX_phieunhaphang_h();


                //string machuhang = this.txtmachuhang.Text;
                IEnumerable<NX_phieunhaphang_h> list = from c in context.NX_phieunhaphang_hs where c.sophieu.Equals(sophieu) select c;



                // MessageBox.Show("Bạn chắc chắn muốn xóa?", "Xác nhận",MessageBoxButtons.YesNo);





                foreach (NX_phieunhaphang_h i in list)
                {
                    context.NX_phieunhaphang_hs.DeleteOnSubmit(i);
                }
                context.SubmitChanges();


           
        }

        private void btNext_Click(object sender, EventArgs e)
        {
            try
            {
                this.grdata.Rows[currenrowindex].Selected = false;
                if (currenrowindex < this.grdata.Rows.Count - 1)
                {
                    currenrowindex = currenrowindex + 1;
                }

                this.grdata.Rows[currenrowindex].Selected = true;

                getrowdata();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }



        private void getrowdata()
        {
            
            NX_phieunhaphang_h h = new NX_phieunhaphang_h();
           
            if (currenrowindex < this.grdata.Rows.Count )
            {
                String sophieu = grdata.Rows[currenrowindex].Cells["sophieu"].Value.ToString();
                IEnumerable<NX_phieunhaphang_h> list = from c in context.NX_phieunhaphang_hs where c.sophieu.Equals(sophieu) select c;
               
                if (list.Count() > 0)
                {    h = list.ElementAt(0);
                    this.txtsophieu.Text = h.sophieu;
                    this.txtmachuhang.Text = h.machuhang;
                    IEnumerable<DM_chuhang> l = from c in context.DM_chuhangs where c.machuhang.Equals(h.machuhang) select c;
                    if (l.Count() > 0)
                    {
                        DM_chuhang ch = l.ElementAt(0);
                        this.txttenchuhang.Text = ch.tenchuhang;
                        this.txtdiachi.Text = ch.diachi;

                    }

                    this.txtsoxe.Text = h.soxe;
                    this.txtlaixe.Text = h.laixe;
                    this.dtpgiovao.Value = DateTime.Parse(h.giovao);
                    this.dtpgiora.Value = DateTime.Parse(h.giora);
                    this.cbloaimi.SelectedValue = h.loaimi;
                    this.txttongtrongluong.Text = h.tongtrongluong.ToString();
                    this.txttrongluongxe.Text = h.trongluongxe.ToString();
                    double trongluonghang = (double)(h.tongtrongluong - h.trongluongxe);

                    this.txttrongluonghang.Text = trongluonghang.ToString();
                    //string sophieu = h.sophieu;
                    bool flag = true;
                    IEnumerable<NX_phieunhaphang_l> kiemdinhs = from c in context.NX_phieunhaphang_ls where c.sophieu.Equals(h.sophieu) select c;
                    if (kiemdinhs.Count() > 0)
                    {
                        flag = false;
                    }
                    IEnumerable<KT_hoadon_l> hoadons = from c in context.KT_hoadon_ls where c.sophieunhap.Equals(h.sophieu) select c;
                    if (hoadons.Count() > 0)
                    {

                        flag = false;
                    }

                    if (flag == false)
                    {
                        this.btDelete.Enabled = false;
                        this.btSave.Enabled = false;
                        this.bttrongluongbi.Enabled = false;
                        this.bttrongluongxoi.Enabled = false;
                        this.lbstatus.Text = "Trạng thái:Xem phiếu";
                    }
                    else
                    {
                        this.btDelete.Enabled = true;
                        this.btSave.Enabled = true;
                        this.bttrongluongxoi.Enabled = true;
                        this.bttrongluongbi.Enabled = true;
                        this.lbstatus.Text = "Trạng thái:Chỉnh sửa phiếu";
                    }
                    //this.txtmahang.Text = hh.mahang;
                    // this.txttenhang.Text = hh.tenhang;
                }
            }
           
        }


        private void clear()
        {
            this.txtsophieu.Text = "";
            this.txtmachuhang.Text ="";
            this.txttenchuhang.Text = "";
            this.txtdiachi.Text = "";
            this.txtsoxe.Text = "";
            this.txtlaixe.Text = "";
            this.dtpgiovao.Value = DateTime.Today;
            this.dtpgiora.Value = DateTime.Today;
            


 
        }

        private void btPrevious_Click(object sender, EventArgs e)
        {
            try
            {
                this.grdata.Rows[currenrowindex].Selected = false;
                if (currenrowindex > 0)
                {
                    currenrowindex = currenrowindex - 1;
                }

                this.grdata.Rows[currenrowindex].Selected = true;

                getrowdata();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btLast_Click(object sender, EventArgs e)
        {
            try
            {
                this.grdata.Rows[currenrowindex].Selected = false;

                currenrowindex = this.grdata.Rows.Count - 1;


                this.grdata.Rows[currenrowindex].Selected = true;

                getrowdata();
            }
            catch (Exception ex)
            
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            try
            {
                clear();
                phatsinhsophieu();
                this.txtmachuhang.Focus();
                this.btSave.Enabled = true;
                this.bttrongluongbi.Enabled = true;
                this.bttrongluongxoi.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            try
            {
               
                
                   

                    MODULE_TIENICH.cls_tienich cls = new MODULE_TIENICH.cls_tienich();
                    cls.ShowForm("frm_timkiemchuhang");

                    this.txtmachuhang.Text = cls._makhachhang;
                    IEnumerable<DM_chuhang> chuhangs = from c in context.DM_chuhangs where c.machuhang.Equals(this.txtmachuhang.Text) select c;
                    if (chuhangs.Count() > 0)
                    {
                        DM_chuhang chuhang = chuhangs.ElementAt(0);
                        this.txttenchuhang.Text = chuhang.tenchuhang;
                        this.txtdiachi.Text = chuhang.diachi;
                    }
                    
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private double chuyenchuoidoctubancanthanhso(string s)
        {
            double kq=0;
            string stemp;
            string snumber;
            try
            {
               
                stemp = strongluong.Substring(6, strongluong.Length - 6);
                stemp = stemp.Substring(0, stemp.Length - 3);   
                strongluong = stemp;
                snumber = stemp;
                kq = double.Parse(snumber);
               
            }
            catch (Exception ex)
            {
                MessageBox.Show("Chuoi doc duoc:" + strongluong);

                MessageBox.Show(ex.Message);
            }
            return kq;
 
        }
        private void bttrongluongxoi_Click(object sender, EventArgs e)
        {
            
            XOI = true;
            BI = false;
            if (OK == true)
            {
                OK = false;
            }
            else
            {
                OK = true;
            }

            if (OK == false)
            {
                //double kq = chuyenchuoidoctubancanthanhso(strongluong);
                //this.txttongtrongluong.Text = kq.ToString();
                this.bttrongluongxoi.Text = "Lấy số liệu";
                this.bttrongluongxoi.Image = MODULE_NHAPXUAT.Properties.Resources.Import;
            }
            else
            {
                this.bttrongluongxoi.Text = "Chấp nhận";
                this.bttrongluongxoi.Image = MODULE_NHAPXUAT.Properties.Resources.CHECKMRK;
            }
         

        }

        private void bttrongluongbi_Click(object sender, EventArgs e)
        {
           // MODULE_NHAPXUAT.cls_nhapxuat cls = new MODULE_NHAPXUAT.cls_nhapxuat();
           // cls.ShowForm("frm_bancan");
           // //if (cls.getsetispass == true)
           // //{
           //     this.txttrongluongxe.Text = cls.getsettrongluong;
           //// }
            XOI = false;
            BI = true;
            if (OK == true)
            {
                OK = false;
            }
            else
            {
                OK = true;
            }
            if (OK == false)
            {
                //double kq = chuyenchuoidoctubancanthanhso(strongluong);
                //this.txttrongluongxe.Text = kq.ToString();
                this.bttrongluongbi.Text = "Lấy số liệu";
                this.bttrongluongbi.Image = MODULE_NHAPXUAT.Properties.Resources.Import;
            }
            else
            {
                this.bttrongluongbi.Text = "Chấp nhận";
                this.bttrongluongbi.Image = MODULE_NHAPXUAT.Properties.Resources.CHECKMRK;
            }
        }

        private void btKCS_Click(object sender, EventArgs e)
        {

          
        }

        private void grdata_Click(object sender, EventArgs e)
        {
           
        }

        private void grdata_Click_1(object sender, EventArgs e)
        {
            try
            {
                currenrowindex = this.grdata.CurrentRow.Index;
                this.grdata.Rows[currenrowindex].Selected = true;
                getrowdata();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message);
            }
        }

        private void grdata_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //if (e.ColumnIndex == 0)
            //{
               
            //    if (grdata.Rows[e.RowIndex].Cells["chk"].Value != null)
            //    {
            //        grdata.Rows[e.RowIndex].Cells["chk"].Value = null;
            //    }
            //    else
            //    {
            //        grdata.Rows[e.RowIndex].Cells["chk"].Value = true;
            //    }

            //}
        }

        private void grdata_CellBorderStyleChanged(object sender, EventArgs e)
        {

        }

        private void grdata_Validated(object sender, EventArgs e)
        {
            
        }

        private void grdata_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                //this.grdata.Rows[currenrowindex].Selected = false;
                if (currenrowindex < this.grdata.Rows.Count - 1)
                {
                    currenrowindex = currenrowindex + 1;
                }

                this.grdata.Rows[currenrowindex].Selected = true;

                getrowdata();

              
            }
            if (e.KeyCode == Keys.Up)
            {
                //this.grdata.Rows[currenrowindex].Selected = false;
                if (currenrowindex > 0)
                {
                    currenrowindex = currenrowindex - 1;
                }

                this.grdata.Rows[currenrowindex].Selected = true;

                getrowdata();
            }
        }

        private void dtptungay_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (dtptungay.Value > dtpdenngay.Value)
                {
                    this.dtpdenngay.Value = this.dtptungay.Value;

                }
                //DateTime tungay = this.dtptungay.Value;
                //DateTime denngay = this.dtpdenngay.Value;

                loaddanhsachphieunhap();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dtpdenngay_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (dtpdenngay.Value < dtptungay.Value)
                {
                    this.dtptungay.Value = this.dtpdenngay.Value;

                }
                //DateTime tungay = this.dtptungay.Value;
                //DateTime denngay = this.dtpdenngay.Value;

                loaddanhsachphieunhap();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
      

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                string stest = "";
                if (isloaded == true)
                {
                    if (chedocan == constant.CANTUDONG)
                    {
                        strongluong = this.serialPort1.ReadLine();
                        
                        double kq = 0;
                        try
                        {
                            kq = chuyenchuoidoctubancanthanhso(strongluong);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Loi trong qua trinh chuyen doi du lieu doc duoc tu cong com");
                        }
                        this.lbsolieu.Text = kq.ToString();
                            if (XOI == true)
                            {
                                this.txttongtrongluong.Text = kq.ToString();
                            }
                            if (BI == true)
                            {
                                this.txttrongluongxe.Text = kq.ToString();
                            }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Loi doc cong com"+ex.Message);
            }
            
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtkeyword_TextChanged(object sender, EventArgs e)
        {
            string keyword = txtkeyword.Text;

            
            //IEnumerable<NX_phieunhaphang_h> list = from c in context.NX_phieunhaphang_hs where c.soxe.StartsWith(keyword) where DateTime.Parse(c.giovao)>=this.dtptungay.Value 
            //                                       where DateTime.Parse(c.giovao)<=this.dtpdenngay.Value
            //                                       select c ;
            //this.grdata.Rows.Clear();
            //for (int i = 0; i < list.Count(); i++)
            //{
            //    NX_phieunhaphang_h item = list.ElementAt(i);

            //    if (item.sophieu != null)
            //    {

            //        this.grdata.Rows.Add();

            //        this.grdata.Rows[i].Cells["sophieu"].Value = item.sophieu;
            //        this.grdata.Rows[i].Cells["soxe"].Value = item.soxe;
            //        this.grdata.Rows[i].Cells["tenchuhang"].Value = item.DM_chuhang.tenchuhang;//ch.tenchuhang;
            //        this.grdata.Rows[i].Cells["tongtrongluong"].Value = item.tongtrongluong;
            //        this.grdata.Rows[i].Cells["trongluongxe"].Value = item.trongluongxe;
            //        //Kiem tra tinh trang phieu

            //        grdata.Rows[i].DefaultCellStyle.ForeColor = Color.Red;//Mau phieu moi nhap
            //        IEnumerable<NX_phieunhaphang_l> kiemdinhs = from c in context.NX_phieunhaphang_ls where c.sophieu.Equals(item.sophieu) select c;
            //        if (kiemdinhs.Count() > 0)
            //        {
            //            grdata.Rows[i].Cells["chk"].ReadOnly = true;

            //            grdata.Rows[i].DefaultCellStyle.ForeColor = Color.MediumBlue;//System.Drawing.Color.Red;
            //        }
            //        IEnumerable<KT_hoadon_l> hoadons = from c in context.KT_hoadon_ls where c.sophieunhap.Equals(item.sophieu) select c;
            //        if (hoadons.Count() > 0)
            //        {
            //            grdata.Rows[i].Cells["chk"].ReadOnly = true;
            //            grdata.Rows[i].DefaultCellStyle.ForeColor = Color.Black;//System.Drawing.Color.Red;

            //            //flag = false;
            //        }


            //    }


            //}

            string tungay, denngay;
            string d, m, y;

            if (this.dtptungay.Value.Day >= 10)
            {
                d = this.dtptungay.Value.Day.ToString();
            }
            else
            {
                d = "0" + this.dtptungay.Value.Day.ToString();
            }

            if (this.dtptungay.Value.Month >= 10)
            {
                m = this.dtptungay.Value.Month.ToString();
            }
            else
            {
                m = "0" + this.dtptungay.Value.Month.ToString();
            }
            y = this.dtptungay.Value.Year.ToString();
            tungay = d + "/" + m + "/" + y;
            if (this.dtpdenngay.Value.Day >= 10)
            {
                d = this.dtpdenngay.Value.Day.ToString();
            }
            else
            {
                d = "0" + this.dtpdenngay.Value.Day.ToString();
            }

            if (this.dtpdenngay.Value.Month >= 10)
            {
                m = this.dtpdenngay.Value.Month.ToString();
            }
            else
            {
                m = "0" + this.dtpdenngay.Value.Month.ToString();
            }
            y = this.dtpdenngay.Value.Year.ToString();
            denngay = d + "/" + m + "/" + y;



            List<NX_danhsachphieunhapResult> list = (from c in context.NX_danhsachphieunhap(tungay, denngay) 
                                                    where c.soxe.StartsWith(keyword)
                                                    select c).ToList();
            this.grdata.Rows.Clear();
            for (int i = 0; i < list.Count(); i++)
            {
                NX_danhsachphieunhapResult item = list.ElementAt(i);

                if (item.sophieu != null)
                {

                    this.grdata.Rows.Add();

                    this.grdata.Rows[i].Cells["sophieu"].Value = item.sophieu;
                    this.grdata.Rows[i].Cells["soxe"].Value = item.soxe;
                    IEnumerable<DM_chuhang> chuhangs = from c in context.DM_chuhangs where c.machuhang.Equals(item.machuhang) select c;
                    if (chuhangs.Count() > 0)
                    {
                        this.grdata.Rows[i].Cells["tenchuhang"].Value = chuhangs.ElementAt(0).tenchuhang;//ch.tenchuhang;
                    }
                    this.grdata.Rows[i].Cells["tongtrongluong"].Value = item.tongtrongluong;
                    this.grdata.Rows[i].Cells["trongluongxe"].Value = item.trongluongxe;
                    //Kiem tra tinh trang phieu

                    grdata.Rows[i].DefaultCellStyle.ForeColor = Color.Red;//Mau phieu moi nhap
                    IEnumerable<NX_phieunhaphang_l> kiemdinhs = from c in context.NX_phieunhaphang_ls where c.sophieu.Equals(item.sophieu) select c;
                    if (kiemdinhs.Count() > 0)
                    {
                        grdata.Rows[i].Cells["chk"].ReadOnly = true;

                        grdata.Rows[i].DefaultCellStyle.ForeColor = Color.MediumBlue;//System.Drawing.Color.Red;
                    }
                    IEnumerable<KT_hoadon_l> hoadons = from c in context.KT_hoadon_ls where c.sophieunhap.Equals(item.sophieu) select c;
                    if (hoadons.Count() > 0)
                    {
                        grdata.Rows[i].Cells["chk"].ReadOnly = true;
                        grdata.Rows[i].DefaultCellStyle.ForeColor = Color.Black;//System.Drawing.Color.Red;

                        //flag = false;
                    }


                }


            }


        }

        private void btPrint_Click(object sender, EventArgs e)
        {
            string sophieu = this.txtsophieu.Text;
            frm_inphieukiemdinh frm = new frm_inphieukiemdinh();
            frm._sophieu = sophieu;
            frm.Show();

        }

        private void txttongtrongluong_TextChanged(object sender, EventArgs e)
        { double tongtrongluong;
            try
            {
                 tongtrongluong = double.Parse(this.txttongtrongluong.Text);
            }catch(Exception ex)
            {
                tongtrongluong = 0;
            }
            double trongluongxe;
            try
            {
                trongluongxe = double.Parse(this.txttrongluongxe.Text);
            }
            catch (Exception ex)
            {
                trongluongxe = 0;
            }
            double trongluonghang = tongtrongluong - trongluongxe;
            this.txttrongluonghang.Text = trongluonghang.ToString();
        }

        private void txttrongluongxe_TextChanged(object sender, EventArgs e)
        {
            double tongtrongluong;
            try
            {
                tongtrongluong = double.Parse(this.txttongtrongluong.Text);
            }
            catch (Exception ex)
            {
                tongtrongluong = 0;
            }
            double trongluongxe;
            try
            {
                trongluongxe = double.Parse(this.txttrongluongxe.Text);
            }
            catch (Exception ex)
            {
                trongluongxe = 0;
            }
            double trongluonghang = tongtrongluong - trongluongxe;
            this.txttrongluonghang.Text = trongluonghang.ToString();
        }

        private void grdata_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }


       
    }
}
